package com.bsoft.gol.editor.controller;

import com.alibaba.fastjson.JSONObject;
import hcn.util.BaseResponse;
import hcn.util.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.bsoft.gol.editor.utils.CommonUtils;
import com.bsoft.gol.editor.utils.FileUploadUtil;
import com.bsoft.gol.editor.utils.HttpPostUploadUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * @version v0.1
 * @description 文件上传
 * @create 2018-12-02 16:59
 **/
@Controller
@RequestMapping(value = "/api/upload")
public class EditorFileController {
    private static final Logger LOGGER = LoggerFactory.getLogger(EditorFileController.class);

    @Autowired
    HttpPostUploadUtil imageUploadService;

    @Value("${image_url}")
    private String fileUploadUrl;

    @Value("${image_url}/fileDownloadByFilePath")
    private String fileDownUrl;

    @Value("${image_url}/")
    private String netServiceUrl;

    @Value("${access_token}")
    private String accessToken;

    /**
     * @param file
     * @param request
     * @throws
     * @description: 图片上传
     * @return: java.com.com.bsoft.gol.editor.editor.util.Map
     * @author: tongtp
     * @date: 2018/12/2 17:08
     */
    @RequestMapping(value = "image")
    @ResponseBody
    public BaseResponse uploadImage(@RequestParam(value = "file", required = false) CommonsMultipartFile file,
                                           HttpServletRequest request) {
        return upload(file, request);
    }

    /**
     * @param file
     * @param request
     * @throws
     * @description: 图片上传
     * @return: java.com.com.bsoft.gol.editor.editor.util.Map
     * @author: tongtp
     * @date: 2018/12/2 17:08
     */
    @RequestMapping(value = "file")
    @ResponseBody
    public BaseResponse uploadFile(@RequestParam(value = "file", required = false) CommonsMultipartFile file,
                                      HttpServletRequest request) {
        return upload(file, request);
    }

    public BaseResponse upload(CommonsMultipartFile file,HttpServletRequest request){
        BaseResponse baseResponse = new BaseResponse();
        String url = fileUploadUrl;

        String token = request.getParameter("token");;

        LOGGER.info("accessToken="+token);
        if(CommonUtils.isEmpty(token)){
            token = accessToken;
        }


        JSONObject jsonObject = FileUploadUtil.formUploadToHcn(url, token,file);

        if (!jsonObject.getBoolean("success") || jsonObject.getJSONObject("record") == null) {
            baseResponse.setCode(ResultCode.ERROR);
            baseResponse.setMessage("上传文件失败！");
            return baseResponse;
        }
        JSONObject jsonObjectRes = new JSONObject();
        jsonObjectRes.put("remark", jsonObject.getJSONObject("record").get("fileName"));
        jsonObjectRes.put("fileurl", netServiceUrl + jsonObject.getJSONObject("record").get("fileId"));
        baseResponse.setData(jsonObjectRes);
        baseResponse.setMessage("上传文件成功");

        return baseResponse;
    }
}
